package com.jr.controller;


import com.google.gson.Gson;
import com.jr.entity.User;
import com.jr.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;


/**
 * 用户信息表
 *
 * @author Snoopy
 * @since 1.0.0 2022-12-31
 */
@Controller
@RequestMapping("/user/")
@SuppressWarnings("all")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("login")
    public @ResponseBody String loginCheck(User user, HttpSession session){

        User user1 = userService.checkUser(user);
        Gson gson = new Gson();
        if(user.getUpassword()!=null){
            session.setAttribute("user",user1);
            session.setAttribute("user1",gson.toJson(user1));
        }
        if(user1!=null&&(user1.getUname()==null||user1.getUemail()==null||user1.getUphone()==null
                ||"".equals(user1.getUname())||"".equals(user1.getUemail())||"".equals(user1.getUphone()))){
            session.setAttribute("flag","false");
        }
        Boolean bol = user1!=null;
        if(bol){
            return "true";
        }else {
            return "false";
        }
    }

    @RequestMapping("register")
    public @ResponseBody String registerCheck(User user){
        user.setUid(String.valueOf(new Date().getTime()));
        if(userService.registerUser(user))
            return "true";
        return "exist";
    }

    @RequestMapping("logout")
    public void registerCheck(HttpSession session){
        session.removeAttribute("user1");
        session.removeAttribute("user");
        session.removeAttribute("flag");
    }

    @PostMapping(value = "queryusername",produces = "application/json;charset=utf-8;")
    public @ResponseBody User getUserName(User user){
        User user1 = userService.checkUser(user);
        if (user1!=null)
            return user1;
        return user;
    }

    @PostMapping(value = "updatePassword")
    public @ResponseBody String updatePassword(User user,HttpSession session){
        if(userService.changeUserInfo(user,session))
            return "true";
        return "false";
    }

    @GetMapping("personalResume")
    public String toResume(){
        return "personal_resume";
    }


    @GetMapping("personalSelected")
    public String personalSelected(){
        return "personal_selected";
    }

    @ResponseBody
    @PostMapping(value = "changeUser")
    public String changeUser(User user, HttpSession session){
        session.removeAttribute("flag");
        User user1 = (User)session.getAttribute("user");
        user.setUid(user1.getUid());
        System.out.println(user);
        boolean success = userService.changeUserInfo(user,session);
        if (success){
            return "1";
        }
        return "0";
    }
}